package org.ibase4j.system.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ibase4j.jwt.utils.StringUtils;
import org.ibase4j.system.model.SysDic;
import org.ibase4j.system.service.SysDicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.util.InstanceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 *
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:14:34
 */
@RestController
@Api(value = "字典管理", description = "字典管理")
@RequestMapping(value = "/dic")
public class SysDicController extends BaseController<SysDic, SysDicService> {

    @ApiOperation(value = "查询字典项")
    @RequiresPermissions("sys.base.dic.read")
    @RequestMapping(value = "/read/dic")
    public Object queryList(ModelMap modelMap, String type,String format) {
        Map<String, Object> dicParam = InstanceUtil.newHashMap();
        dicParam.put("type", type);
        dicParam.put("orderBy", "sort_no asc");
        List<SysDic> sysDicsList = service.queryList(dicParam);
        List<Map<String,Object>> dicMapList = new ArrayList<>();
        for (SysDic sysDic : sysDicsList){
            Map<String,Object> oneDicMap = new HashMap<>();
            if(StringUtils.equalsIgnoreCase("int",format)){
                oneDicMap.put("value", Integer.parseInt(sysDic.getCode()));
            }else if(StringUtils.equalsIgnoreCase("long",format)){
                oneDicMap.put("value", Long.parseLong(sysDic.getCode()));
            }else{
                oneDicMap.put("value", sysDic.getCode());
            }
            oneDicMap.put("label",sysDic.getCodeText());
            dicMapList.add(oneDicMap);
        }
        return setSuccessModelMap(modelMap,dicMapList);
    }

    @Override
    @ApiOperation(value = "查询字典项")
    @RequiresPermissions("sys.base.dic.read")
    @PostMapping(value = "/read/page")
    public Object query(ModelMap modelMap, @RequestBody Map<String, Object> param) {
        param.put("orderBy", "type_,sort_no");
        return super.query(modelMap, param);
    }

    @Override
    @ApiOperation(value = "查询字典项")
    @RequiresPermissions("sys.base.dic.read")
    @GetMapping(value = "/read/list")
    public Object queryList(ModelMap modelMap, @RequestBody Map<String, Object> param) {
        param.put("orderBy", "type_,sort_no");
        return super.queryList(modelMap, param);
    }

    @ApiOperation(value = "字典项详情")
    @RequiresPermissions("sys.base.dic.read")
    @GetMapping(value = "/read/detail")
    public Object get(ModelMap modelMap, @RequestBody SysDic param) {
        return super.get(modelMap, param);
    }

    @Override
    @PostMapping(value = "/save")
    @ApiOperation(value = "修改字典项")
    @RequiresPermissions("sys.base.dic.update")
    public Object update(ModelMap modelMap, @RequestBody SysDic param) {
        if (param.getId() != null) {
            SysDic result = service.queryById(param.getId());
            if ("0".equals(result.getEditable())) {
                throw new BusinessException("不允许修改系统内置字典项");
            }
        }
        return super.update(modelMap, param);
    }

    @Override
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除字典项")
    @RequiresPermissions("sys.base.dic.delete")
    public Object delete(ModelMap modelMap, @RequestBody SysDic param) {
        return super.delete(modelMap, param);
    }
}
